package com.common.core.base;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.View;

import com.common.core.CoreConfiguration;
import com.common.core.R;
import com.common.core.bean.TokenInvaliedEventBean;
import com.common.core.manage.CommonActivityManager;
import com.common.core.utils.NetworkUtils;
import com.common.core.utils.ToastUtils;
import com.common.core.view.ProgressHUD;
import com.common.http.basecore.bean.response.HttpBaseResponseUtils;
import com.common.http.basecore.utils.LogInfo;
import com.umeng.analytics.MobclickAgent;

import org.greenrobot.eventbus.EventBus;


/**
 * Created by haitian on 2015/5/21.
 */
public abstract class CommonBaseActivity extends FragmentActivity implements
        View.OnClickListener {
    protected ProgressHUD progress;
    protected String TAG;
    protected TokenInvaliedEventBean mInvaliedEventBean;

    protected boolean isFirstIn = true;
    protected long timeElapse = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TAG = this.getClass().getSimpleName();
        LogInfo.log("TAG", "TAG ="+TAG);
        mInvaliedEventBean = new TokenInvaliedEventBean();
        mInvaliedEventBean.setTag(TAG);
        mInvaliedEventBean.setActivity(this);
        CommonActivityManager.addActicity(TAG, this);
        MobclickAgent.setDebugMode(CoreConfiguration.isDebug());
        MobclickAgent.openActivityDurationTrack(false);// 禁止默认的页面统计方式，这样将不会再自动统计Activity。
//        MobclickAgent.setScenarioType(this, MobclickAgent.EScenarioType.E_UM_NORMAL);
//        netJudge();
    }
    protected  void  netJudge(){
        if (NetworkUtils.isNetworkAvailable()) {
            showToast(R.string.common_net_not_availabe);
        }
    }
    @Override
    protected void onResume() {
        super.onResume();
        if(isFirstIn){
            isFirstIn = false;
        }else {
            timeElapse = System.currentTimeMillis() - timeElapse;
        }
        MobclickAgent.onPageStart(getClass().getSimpleName());
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        timeElapse = System.currentTimeMillis();
        MobclickAgent.onPageEnd(getClass().getSimpleName());
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        CommonActivityManager.removeActicity(TAG);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }

    @Override
    public void onClick(View v) {

    }

    /**
     * 普通toast
     *
     * @param msg
     */
    protected void showToast(String msg) {
        ToastUtils.showToast(msg);
    }

    /**
     * 普通toast
     */
    protected void showToast(int resId) {
        ToastUtils.showToast(resId);
    }

    /**
     * 加对错符合的toast
     *
     * @param msg
     * @param b
     */
    protected void showToast(String msg, Boolean b) {
        if (b) {
            ToastUtils.showToast(msg);
        } else {
            ToastUtils.showToast(msg);
        }

    }

    /**
     * 异常的统一处理
     *
     * @param ex
     */
    public void handleException(Throwable ex) {
        if (ex instanceof HttpBaseResponseUtils.NetWorkTipException) {
            showToast(ex.getMessage());
        } else if (ex instanceof HttpBaseResponseUtils.TipException) {
            showToast(ex.getMessage());
        } else if (ex instanceof HttpBaseResponseUtils.NoneTipException) {
            showToast(ex.getMessage());
        } else if (ex instanceof HttpBaseResponseUtils.TokenInvalidException) {
            mInvaliedEventBean.setToastMsg(getString(R.string.token_invalid_exception_message));
            EventBus.getDefault().post(mInvaliedEventBean);
//            tokenInvalid(getString(R.string.token_invalid_exception_message));
        }
//        else if (ex instanceof SocketException || ex instanceof IOException) {
//            ex.printStackTrace();
//            if (NetworkUtils.isNetworkAvailable()) {
//                showToast(R.string.common_net_not_availabe);
//            } else {
//                showToast(R.string.none_tip_exception_message);
//            }
//        }
        else {
            ex.printStackTrace();
            if (NetworkUtils.isNetworkAvailable()) {
                showToast(R.string.common_net_not_availabe);
            } else {
                showToast(R.string.none_tip_exception_message);
            }
        }
        hideDialog();
    }

    /**
     * 网络请求统一的错误处理
     *
     * @param ex     异常
     * @param exDesc 未知异常的描述
     */
    public void handleException(Throwable ex, String exDesc) {
        String exSoureMsg = ex.getMessage();
        if (ex instanceof HttpBaseResponseUtils.NetWorkTipException) {
            showToast(exSoureMsg);
        } else if (ex instanceof HttpBaseResponseUtils.TipException) {
            showToast(exSoureMsg);
        } else if (ex instanceof HttpBaseResponseUtils.NoneTipException) {
            showToast(exSoureMsg);
        } else if (ex instanceof HttpBaseResponseUtils.TokenInvalidException) {
            if (TextUtils.isEmpty(exSoureMsg)) {
                exDesc = getString(R.string.token_invalid_exception_message);
            } else {
                exDesc = exSoureMsg;
            }
//            tokenInvalid(exDesc);
            mInvaliedEventBean.setToastMsg(exDesc);
            EventBus.getDefault().post(mInvaliedEventBean);
        }
//        else if (ex instanceof SocketException || ex instanceof IOException) {
//            ex.printStackTrace();
//            showToast(getString(R.string.none_tip_exception_message));
//        }
        else {
            ex.printStackTrace();
            if (TextUtils.isEmpty(exDesc)) {
                if (NetworkUtils.isNetworkAvailable()) {
                    showToast(R.string.common_net_not_availabe);
                } else {
                    showToast(R.string.none_tip_exception_message);
                }
            } else {
                if (NetworkUtils.isNetworkAvailable()) {
                    showToast(R.string.common_net_not_availabe);
                } else {
                    showToast(exDesc);
                }
            }
//            }
        }
    }
    protected boolean isBackGroundTimeMT2Min(){
        return timeElapse > 2 * 60 * 1000 ? true :false;
    }
    protected void intent2Activity(Class<? extends Activity> tarActivity) {
        Intent intent = new Intent(this, tarActivity);
        startActivity(intent);
    }

    public void showDialog(String str) {
        progress = ProgressHUD.show(this, str, true);
    }

    public void hideDialog() {

        if (progress != null && !isFinishing()) {
            progress.dismiss();
        }
    }

    public void showDialogNotCanDismiss(String str) {
        progress = ProgressHUD.show(this, str, false);
    }

    public boolean isDialogShow() {
        if (progress != null && !isFinishing()) {
            return progress.isShowing();
        } else {
            return false;
        }
    }

    public TokenInvaliedEventBean getInvaliedEventBean() {
        return mInvaliedEventBean;
    }

    public void setInvaliedEventBean(TokenInvaliedEventBean invaliedEventBean) {
        mInvaliedEventBean = invaliedEventBean;
    }
}
